<template>
  <div class="member-input">
    <img v-if="!member" class="qrcode" :src="this.qrcode" />
    <img v-else class="qrcode" :src="member.avatar" />
    <p class="nickname">
      <template v-if="!member">微信扫一扫添加管理员</template>
      <template v-else>{{member.nickname}}</template>
    </p>

    <!--S二维码过期提示-->
    <div id="qrcode-past"
         @click="createQrcode"
         v-if="qrcode_past && !member"
    >
      <div class="qrcode-past">
        <Icon type="md-refresh" :size="50" color="#fff" />
        <p>二维码过期</p>
      </div>
    </div>
    <!--E二维码过期提示-->

    <!--S关闭-->
    <div id="close" @click="createQrcode" v-if="member">
      <Icon type="md-close" :size="25" color="red" />
    </div>
    <!--E关闭-->

  </div>
</template>

<script>
// 扫码获取前端用户信息
import QRCode from 'qrcode'
let ScanTimer = null
export default {
  name: 'member-input',
  data () {
    return {
      // 用户信息
      member: null,
      // 二维码
      qrcode: '',
      // 二维码是否过期
      qrcode_past: false
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: '',
    toggle: ''
  },
  watch: {
    toggle (value) {
      if (value > 0 && !this.value) {
        this.createQrcode()
      } else if (value > 0 && this.value) {
        this.getMember()
      } else if (value < 0) {
        this.member = null
        clearInterval(ScanTimer)
      }
    }
  },
  mounted () {
    clearInterval(ScanTimer)
  },
  destroyed () {
    clearInterval(ScanTimer)
  },
  methods: {
    /**
     * 创建二维码
     */
    createQrcode () {
      this.qrcode_past = false
      this.member = null
      this.$emit('change', '')
      this.getQrcodeUrl()
        .then((res) => {
          let { code, data } = res.data
          if (!code) {
            QRCode.toDataURL(data, {
              margin: 1
            }, (error, url) => {
              if (!error) {
                this.qrcode = url
                this.listenScan()
              }
            })
          }
        })
    },
    /**
     * 获取二维码
     */
    getQrcodeUrl () {
      return this.$get('memberInput/getQrcodeUrl', {
        data: {
          scan_type: 'set_manage'
        }
      })
    },
    /**
     * 监听二维码是否过期&&用户扫码
     */
    listenScan () {
      clearInterval(ScanTimer)
      ScanTimer = setInterval(() => {
        this.$get('memberInput/testPast', {
          data: {
            scan_type: 'set_manage'
          }
        })
          .then((res) => {
            let { code } = res.data
            if (code) {
              this.qrcode_past = true
              clearInterval(ScanTimer)
            }
          })
        this.$get('memberInput/listenScan')
          .then((res) => {
            let { code, data } = res.data
            if (!code) {
              this.member = {
                avatar: data.avatar,
                nickname: data.nickname,
                uid: data.uid
              }
              this.$emit('change', data.uid)
              clearInterval(ScanTimer)
            }
          })
      }, 500)
    },
    /**
     * 获取管理员头像昵称
     */
    getMember () {
      this.$get('memberInput/getAvatarNickname', {
        uid: this.value
      })
        .then((res) => {
          let { code, data } = res.data
          if (!code) {
            this.member = {
              avatar: data.avatar,
              nickname: data.nickname
            }
          }
        })
    }
  }
}
</script>

<style scoped lang="less">
  .member-input {
    position: relative;
    width: 130px;
    clear: both;
    line-height: 100%;
    img.qrcode{
      width: 100%;
      height: 130px;
    }
    p.nickname{
      height: 20px;
      line-height: 20px;
      text-align: center;
    }
    #qrcode-past{
      position: absolute;
      top:0;
      left:0;
      width: 130px;
      height: 130px;
      background: rgba(0,0,0,.7);
      cursor: pointer;
      .qrcode-past{
        text-align: center;
        position: absolute;
        width: 100%;
        top:50%;
        transform: translateY(-50%);
        color: #fff;
      }
    }
    #close{
      position: absolute;
      top:0;
      right: 0;
      cursor: pointer;
    }
  }
</style>
